package com.qianniu.napi.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.League;
import com.qianniu.napi.admin.entity.Store;
import com.qianniu.napi.admin.mapper.LeagueMapper;
import com.qianniu.napi.admin.mapper.StoreMapper;
import com.qianniu.napi.admin.service.IStoreService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qianniu.napi.admin.web.AccountController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实体店商家地址，需要满足实体营业执照+店面+独立小程序 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-10-16
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private LeagueMapper leagueMapper;

    @Override
    public List<Store> getNearbyStores(int pageIndex, int pageSize, double lat, double lng, long mid,String sortby) {

        Map<String,Object> map = new HashMap<String,Object>();

        Wrapper<Store> entity = new EntityWrapper<Store>();
        if(mid>0){
            entity.eq("mid",mid);
            map.put("mid",mid);
        }
        int total = storeMapper.selectCount(entity);

        if(total==0) return null;
        int skip = (pageIndex-1)*pageSize;
        if(Strings.isNullOrEmpty(sortby))sortby="distance";

        map.put("skip",skip);
        map.put("size",pageSize);
        map.put("lat",lat);
        map.put("lng",lng);
        map.put("lng",lng);
        map.put("sortby",sortby);
        String sort = "ASC";
        if(!Strings.isNullOrEmpty(sortby)&&!sortby.equals("distance")){
            sort = "desc";
        }
        map.put("sort",sort);

        return storeMapper.selectNearbyStores(map);
    }

    @Override
    public List<Store> getHyStores(int pageIndex, int pageSize, double lat, double lng, long mid,String sortby,Long hyid) {

        List<Store> list =null;
        try {


            Map<String, Object> map = new HashMap<String, Object>();

            Wrapper<Store> entity = new EntityWrapper<Store>();
            if (mid > 0) {
                entity.eq("mid", mid);
                map.put("mid", mid);
            }
            if (hyid > 0) {
                entity.eq("type", hyid);
                map.put("type", hyid);
            }
            int total = storeMapper.selectCount(entity);
            logger.info("=========total="+total);

            if (total == 0) return null;
            int skip = (pageIndex - 1) * pageSize;
            if (Strings.isNullOrEmpty(sortby)) sortby = "distance";

            map.put("skip", skip);
            map.put("size", pageSize);
            map.put("lat", lat);
            map.put("lng", lng);
            map.put("lng", lng);
            map.put("sortby", sortby);
            String sort = "ASC";
            if (!Strings.isNullOrEmpty(sortby) && !sortby.equals("distance")) {
                sort = "desc";
            }
            map.put("sort", sort);

            list = storeMapper.selectNearbyStores(map);
        }catch (Exception e){
            logger.error("getHyStores query error",e);
        }
        return list;
    }

    public League getNearbyLm(double lat, double lng, long mpid){
        Map map = new HashMap<String,Object>();
        map.put("lat",lat);
        map.put("lng",lng);
        map.put("mpid",mpid);
        return leagueMapper.getNearbyLm(map);
    }
}
